package com.example.sports_meet.event;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class event_add extends AppCompatActivity {
    private Button button;
    private Button button_add;
    private MyDbHelper myDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_add);
        Objects.requireNonNull(getSupportActionBar()).hide();

        List<EditText> editTextList = new ArrayList<>();
        ViewGroup rootView = findViewById(R.id.rootLayout); // 用您界面布局的根视图ID替换R.id.rootLayout
        traverseEditTexts(rootView, editTextList);

        myDbHelper = new MyDbHelper(this, "mydb", null, 1);
        SQLiteDatabase db = myDbHelper.getWritableDatabase();
//        db.delete("form", null, null);
//        myDbHelper.insertFormData(db);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        button_add = findViewById(R.id.button_add);
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String id = editText1.getText().toString();
//                String name = editText2.getText().toString();
                for (EditText i : editTextList) {
                    if (i.getText().toString().length() == 0) {
                        Toast.makeText(event_add.this, "请输入内容", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (!editTextList.get(0).getText().toString().matches("\\d+")) {
                    Toast.makeText(event_add.this, "编号只能为数字", Toast.LENGTH_SHORT).show();
                } else {
                    SQLiteDatabase db = myDbHelper.getWritableDatabase();
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("eno", editTextList.get(0).getText().toString());
                    contentValues.put("ename", editTextList.get(1).getText().toString());
                    contentValues.put("etime", editTextList.get(2).getText().toString());
                    contentValues.put("fno", editTextList.get(3).getText().toString());
                    contentValues.put("jno", editTextList.get(4).getText().toString());
                    contentValues.put("sno", editTextList.get(5).getText().toString());
                    contentValues.put("esex", editTextList.get(6).getText().toString());

                    if (db.insert("eevent", null, contentValues) >= 1) {
                        Toast.makeText(event_add.this, "添加成功", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
    }

    private void traverseEditTexts(ViewGroup viewGroup, List<EditText> editTextList) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View childView = viewGroup.getChildAt(i);
            if (childView instanceof EditText) {
                editTextList.add((EditText) childView);
            } else if (childView instanceof ViewGroup) {
                traverseEditTexts((ViewGroup) childView, editTextList);
            }
        }
    }
}